package com.sykj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sykj.bean.Mygold;
import com.sykj.bean.bis.BisCustomUser;
import com.sykj.config.oss.FileUploadUtil;
import com.sykj.mapper.GameLoadMapper;
import com.sykj.mapper.MygoldMapper;
import com.sykj.service.BisCustomUserService;
import com.sykj.util.IDUtils;
import com.sykj.util.RedisByhUtils;
import com.sykj.util.RedisKeys;
import com.sykj.util.redis.RedisUtil;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: BeautyClub
 * @description: 用户信息接口类
 * @author: qiaoli
 * @create: 2020-07-27 10:36
 */
@RestController
@RequestMapping("/bisCustomUser")
@CrossOrigin
public class BisCustomUserController {
    @Autowired
    BisCustomUserService bisCustomUserService;
    @Autowired
    FileUploadUtil fileUploadUtil;
    @Autowired
    RedisByhUtils byhUtils;

    @Autowired
    MygoldMapper mygoldMapper;
   //更据手机号 查询用户信息
    @PostMapping("/queryByid/{openid}")
    public ResultBody queryByid(@PathVariable("openid")String  openid){
        //  判断是否为第一次微信登录
        System.err.println(openid);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("openid",openid);
        BisCustomUser bisCustomUser=bisCustomUserService.getOne(queryWrapper);

        //若不为空，表明已登录过次账号！
        if(bisCustomUser!=null){
            // 将 此账号信息添加到redis中，作为redis 共享 session

            byhUtils.set(RedisKeys.SESSIONID+bisCustomUser.getCustomUserId(),bisCustomUser.getCustomUserId());
         return  new ResultBody(CommonEnum.SUCCESS,bisCustomUser);
        }
        //若没有查到说明此账号第一次登录
        //在reids中查找此账号信息
          boolean b= RedisUtil.hasKey(openid);
        //若查到此账号，将信息添加到数据库中
        if(b==true){
            String object= RedisUtil.get(openid)+"";
            JSONObject  jsonObject= JSON.parseObject(object);
            Integer sex = (Integer) jsonObject.get("sex");
            String customUserId = IDUtils.createID();
            String username = (String) jsonObject.get("nickname");
            String photo = (String) jsonObject.get("headimgurl");
            String openid1 = jsonObject.get("openid")+"";
            BisCustomUser bis = new BisCustomUser();
            bis.setSex(sex + "");
            bis.setCustomUserId(customUserId);
            bis.setUsername(username);
            bis.setPhoto(photo);
            bis.setOpenid(openid1);
            boolean save = bisCustomUserService.save(bis);
            if(save==true){
                // 若添加成功，就将 此账号信息添加到redis中
               // 作为redis 共享 session
                byhUtils.set(RedisKeys.SESSIONID+bis.getCustomUserId(),bis.getCustomUserId());
                return  new ResultBody(CommonEnum.SUCCESS,bis);
            }else {
                return  new ResultBody(CommonEnum.NOT_FOUND);
            }


            }
          return  new ResultBody(CommonEnum.NOT_FOUND);
    }
//  绑定用户手机号
@RequestMapping("/addWechat/{tel}/{openid}")
    public  ResultBody  addWechatMessage(@PathVariable("tel")String  tel,@PathVariable("openid")String openid){

    if(tel!=null && openid!=null){
        ResultBody resultBody = bisCustomUserService.addWechatMessage(tel, openid);
        return resultBody;
    }
        return  new ResultBody(CommonEnum.BODY_NOT_MATCH);
    }

    // 修改用户信息
      @PostMapping("/updateBy")
    public ResultBody  updateByUser(
//            @RequestParam(name="bisCustomUser", required = true)
                    BisCustomUser bisCustomUser){

          System.err.println(bisCustomUser.toString()+"--"+bisCustomUser.getCustomUserId());
        if(bisCustomUser.getCustomUserId()!=null){
            return bisCustomUserService.updateUser(bisCustomUser);
        }else{
            return  new ResultBody(CommonEnum.BODY_NOT_MATCH);
        }

    }
    // 修改用户图片,
    @RequestMapping("/updateByimg/{customUserId}")
    public ResultBody  UpdateByImg(@RequestParam(name="file",required = true) MultipartFile file,@PathVariable(name = "customUserId",required = true)String customUserId){
        return bisCustomUserService.updateImg(file,customUserId);
    }


    /**
     * 功能描述: 退出登录  删除redis 中的 共享 session
     * @Param:
     * @Return:
     * @Author: Feiwo Sicun
     * @Date: 2020/8/6 10:37
     */
    @GetMapping("/quit/{userid}")
     public  ResultBody  Quit(@PathVariable(value = "userid")String userid){
        byhUtils.del(RedisKeys.SESSIONID+userid);
         return new ResultBody(CommonEnum.USER_LOGIN_QUIT);
     }
     @RequestMapping("/querymy/{customUserId}")
     public ResultBody queryMygold(@PathVariable("customUserId")String customUseId){

         Mygold mygold1 =  mygoldMapper.queryBybeautyNum(customUseId);
         Mygold mygold2 =   mygoldMapper.queryBypayNum(customUseId);
         Mygold mygold3 = mygoldMapper.selectByZnum(customUseId);
         Mygold date=new Mygold();
         // 缺少判断，若没有订单应该有值；
            if(mygold2==null){
                date.setPayMoney("0");
            }else{
                date.setPayMoney(mygold2.getPayMoney());
            }

            if(mygold3==null){
                date.setZMoney("0");
            }else{
                date.setZMoney(mygold3.getZMoney());
            }

            if(mygold1==null){
                date.setBeautyNum("0");
            }else{
                date.setBeautyNum(mygold1.getBeautyNum());
            }



         return new ResultBody(CommonEnum.SUCCESS,date);
     }
}
